<?php
declare (strict_types=1);

namespace app\middleware;

use think\Exception;

class Auth
{
    public function handle($request, \Closure $next)
    {
        try {
            $authorization = $request->header('Authorization');
            if (!$authorization) {
                throw new Exception('缺少授权参数！');
            }

            $userInfo = getUserInfo($authorization);
            if (!$userInfo) {
                throw new Exception('授权失效请重新登录！');
            }

            return $next($request);
        } catch (\Exception $e) {
            return jsonReturn(1, $e->getMessage(), [], 401);
        }
    }
}
